//
//  Kylin_BaseTableViewController.m
//  01-LotteryBaseFrame
//
//  Created by Kylin on 16/1/9.
//  Copyright © 2016年 kylin. All rights reserved.
//

#import "Kylin_BaseTableViewController.h"


@implementation Kylin_BaseTableViewController
- (NSMutableArray *)groups{
    if (!_groups) {
        _groups = [NSMutableArray array];
    }
    return _groups;
}
// 创样式
- (instancetype)init{
    
    return [super initWithStyle:UITableViewStyleGrouped];
}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.groups.count;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    Kylin_SettingGroup *group = self.groups[section];
    
    
    return group.items.count;
    
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    Kylin_SettingTableViewCell *cell = [Kylin_SettingTableViewCell cellViewTableView:tableView];
    
    Kylin_SettingGroup *group = self.groups[indexPath.section];
    
    Kylin_SettingItem *item = group.items[indexPath.row];
    
    cell.item = item;
    
    return cell;
    
}

// section头部
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    
    Kylin_SettingGroup *group = self.groups[section];
    
    return group.headerText;
    
}

// section尾部
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
    
    Kylin_SettingGroup *group = self.groups[section];
    return group.footerText;
}


// 跳转控制器 ,选中哪一行时会调用该方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    // 取消选择
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    // 获取选中了 哪一行
    Kylin_SettingGroup *group = self.groups[indexPath.section];
    Kylin_SettingItem *item = group.items[indexPath.row];
    
    if (item.operationBlock) {
        item.operationBlock(indexPath);
    }else if([item isKindOfClass:[Kylin_SettingArrowItem class]]){
        Kylin_SettingArrowItem *arrowItem = (Kylin_SettingArrowItem *)item;
        
        if (arrowItem.desVc) {
            UIViewController *vc = [[arrowItem.desVc alloc]init];
            
            vc.navigationItem.title = arrowItem.title;;
            
            [self.navigationController pushViewController:vc animated:YES];
        }
    }

    
}

@end
